package com.pcj.blog.dao;
/**
 * @param
 * @author pcj
 * @date 2021/12/29 11:29
 * @return
 */

import com.pcj.blog.po.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 *
 * @className BlogRepository
 * @author pengcj
 * @date 2021/12/29 11:29
 */

public interface BlogRepository extends JpaRepository<Blog ,Long >, JpaSpecificationExecutor<Blog> {
    @Query("select b from Blog b where b.recommende=true ")
    List<Blog> findTop(Pageable pageable);
    @Query("select b from Blog b where b.title like ?1 or b.content like ?1 or b.description like ?1")
    Page<Blog> findByQuery(String query,Pageable pageable);
    @Modifying // 删除或修改操作需要加 这个注解  并且在Service调用的地方要加事务注解
    @Query("update Blog b set b.views=b.views+1 where b.id = ?1")
    int updateViews(Long id);
    //要用到数据库方法用function  方法名用单引号  只能用单引号
    //  year 报红不用管  是idea的警告 不影响运行
//    @Query(" select function('date_format',b.updateTime,'%Y') as year from Blog b group by  year order by year desc")
    // value 里面的是原生sql    nativeQuery表示这是一条原生sql   注意不要写updateTime   换成update_time
    @Query(value = "select  date_format(b.update_time,'%Y') as year  from t_blog b GROUP BY year ORDER BY year desc",nativeQuery = true)
    List<String> findGroupYears();
    @Query("select b from Blog b where function('date_format',b.updateTime,'%Y') = ?1")
    List<Blog> findByYear(String year);
}
